﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///
///</summary>
public enum EnemyType
{
    little,
    middle,
    big,
}
public class EnemyFactory : MonoBehaviour
{
   public float intervalTime = 4;
    float nextTime;
    float TotalTime;
	int _enemyNum = 0;
    bool _Openballoons = false;
   
 
    Vector2 pos;
    public GameObject enemyParent;
   // Start is called before the first frame update
    void Awake()
   {
       if (GameObject.FindGameObjectsWithTag("EnemyFactory").Length > 1)
       {
           Destroy(this.gameObject);
       }
       else
       {
           DontDestroyOnLoad(this.gameObject);
       }
   }
   
    private void Update()
    {
        TotalTime += Time.deltaTime;
        if (!GameConfig.IsGameOver)
        {
            if (TotalTime >= nextTime)
            {
                RandomEnemyPos();
                nextTime = TotalTime + intervalTime;
				_enemyNum++;
				if (_enemyNum==2&&intervalTime>=0.9f)
				{
					_enemyNum = 0;
					intervalTime -= 0.05f;
				}
                string EnemyType = RandomEnemy();
                GameObject obj = _SetLoadPath(EnemyType);
                if (enemyParent == null)
                {
                    enemyParent = new GameObject();
                    enemyParent.name = "EnemyParent";
                    enemyParent.transform.SetParent(GameObject.Find("Canvas").transform);
                    enemyParent.transform.SetSiblingIndex(2);
                }
                obj.transform.SetParent(enemyParent.transform);
                obj.transform.position = Camera.main.WorldToScreenPoint(RandomEnemyPos());
                GameConfig.EnemyCount++;
            }
        }
    }
    Vector2 RandomEnemyPos()
    {
        pos = new Vector2(Random.Range(-1.8f, 2), 6);
        return pos;
    }
    GameObject _SetLoadPath(string EnemyType)
    {
		string path = "Enemy/"+ EnemyType;
        Debug.Log(path);
        return GameObjectPool.GetInstance().GetObject(path, EnemyType+"(Clone)");
    }

    string RandomEnemy()
    {
        string type = "Enemy1";
        if (GameConfig.EnemyCount >= 18)
            _Openballoons = true;

        if (_Openballoons)
        {
            int temp = Random.Range(0, 3);
            if (temp == (int)(EnemyType.little))
            {
                type = "Enemy1";
            }
            else if (temp == (int)(EnemyType.middle))
            {
                type = "Enemy2";
            }
            else if (temp == (int)(EnemyType.big))
            {
                type = "Enemy3";
            }
        }
        return type;
    }


    //所有怪物设为false；
    public void EnemyRecycle()
    {
        _Openballoons = false;
		intervalTime = 4f;      
		EnemyComponent[] enemyComponents = enemyParent.GetComponentsInChildren<EnemyComponent>();
		for(int i = 0;i<enemyComponents.Length;i++)
		{
			enemyComponents[i].EnemyRecycle();
		}
    }

    public void EnemyDontDestroy()
    {
        _Openballoons = false;
        intervalTime = 4f;

        EnemyComponent[] enemyComponents = enemyParent.GetComponentsInChildren<EnemyComponent>();
        for (int i = 0; i < enemyComponents.Length; i++)
        {
            enemyComponents[i].transform.SetParent(transform);
            enemyComponents[i].gameObject.SetActive(false);
            Debug.Log("父物体名字:" + enemyComponents[i].transform.parent.name + "本体名字:" + enemyComponents[i].transform.name);
        }

         foreach (Transform child in enemyParent.transform)
         {
             child.SetParent(transform);
             child.gameObject.SetActive(false);
        
         }

       

        Debug.Log("怪物转为不可销毁");
    }
    


	
	public void ClearOneBalloonType()
	{
		EnemyComponent[] enemys;
		enemys = enemyParent.GetComponentsInChildren<EnemyComponent>();
		for(int i = 0; i < enemys.Length; i++)
		{
			enemys[i].ClearBalloonOneType();
		}
	}

	public void ClearOne()
	{
		int temp = 0;
		List<float> disList = new List<float>();
		EnemyComponent[] enemys;
		enemys = enemyParent.GetComponentsInChildren<EnemyComponent>();

		Debug.Log(enemys.Length);
		if (disList.Count == 1)
		{
			enemys[0].BalloonBreak();
			return;
		}
		for (int i = 0; i < enemys.Length; i++)
		{
			disList.Add(enemys[i].GetComponent<EnemyComponent>().ObjAndFloorDistance());
		}
		for (int j = 0; j < disList.Count - 2; j++)
		{
			if (disList[j] < disList[j + 1])
			{
				temp = j;
			}
		}
		enemys[temp].BalloonBreak();
	}
}
